# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#

package CCN::Model::StaticDataTypes;
use strict; use warnings;

=pod

    my $registry = new CCN::Framework::Model::DataType::Registry;
    CCN::Content::Type::populate($registry);

=cut

sub populate($)
{
    my $registry = shift;

    # Taken from CCN::Context.pm
    my $qr_ip       = qr(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3});
    my $qr_name     = qr([a-zA-Z]\w{0,14});
    my $qr_dns      = qr([a-zA-Z][\w.-]+\.[\w.-]{1,126});

    $registry->register(

        # These are specific to voice-app

        'pattern' => {
            # Max length is set to 32; adjust VARCHAR(32) / @pattern in system.sql if changing here.
            'validate'  => sub { $_[1] =~ /^[*#1\d]{1,32}$/ },
            'cleanup'   => sub { uc(trimo($_[1])) },
            'input'     => sub { $_[0]->cgi->textfield(-name=>$_[1],-size=>17,-maxlength=>16,-class=>'validate',-default=>$_[2],-force=>(defined$_[2])) },
        },
        'number' => {
            # Max length is set to 32; adjust VARCHAR(32) / @number in system.sql if changing here.
            'validate'  => sub { $_[1] =~ /^[*#\d]{1,32}$/ },
            'input'     => sub { $_[0]->cgi->textfield(-name=>$_[1],-size=>17,-maxlength=>32,-class=>'validate',-default=>$_[2],-force=>(defined$_[2])) },
        },
        'tonezone' => {  
        },
        'destination' => {
            'input' => sub {
                use CCNCore::Home;
                opendir(my $dh, CCNCore::Home::manager.'/lib/CCNv2/content/e164') or die "Can't opendir ".CCNCore::Home::manager.'/lib/CCNv2/content/e164';
                my @actual = sort grep { !/^\./ } readdir($dh);
                closedir $dh;
                my %actual = map { $a = $_; $a =~ s/\./ /; $_ => $a } @actual;
                $_[0]->cgi->popup_menu(-name=>$_[1],-class=>'validate',-default=>$_[2],-force=>(defined$_[2]), 
                    -values=>['','emergency','default',@actual], -labels=>{''=>'','emergency'=>'Emergency', 'default'=>'Last Resort',%actual}); 
            },

        },
        'pattern_or_name' => {  # A "pattern" or "name"
        },
        'number_or_name' => {
        },
        'route' => {
            'validate' => sub { $_[1] =~ /^[\w-]+$/ },
        },
        'cos' => {
            'validate' => sub { $_[1] =~ /^[\w-]+$/ },
        },
        'map' => {
            'validate' => sub { $_[1] =~ /^[\w-]+$/ },
        },
        'context' => {
            'validate' => sub { $_[1] =~ /^[\w-]+$/ },
        },

        
    );
}

1;
